//headers 
#include<algorithm>
#include<cassert>
#include<cstdio>
#include<queue>
#include<deque>
#include<iostream>
#include<list>
#include<map>
#include<numeric>
#include<deque>
#include<set>
#include<string>
#include<sstream>
#include<stack>
#include<streambuf>
#include<vector>
using namespace std;

//macros
#define LET(x,a) typeof(a) x(a)
#define FOR(i,a,n) for(LET(i,a);i!=n;i++)
#define REP(i,n) FOR(i,0,n)
#define sz size()
#define pb push_back
#define ALL(x) x.begin(),x.end()
#define PRINT(x) REP(_,x.sz) cout<<x[_]<<" "; cout<<endl;
const int INF = (int) 1e8;
#define VI vector<int>
#define VS vector<string>
#define VVI vector< vector<int> >
#define PII pair<int,int>
#define VII vector< PII >
#define LL long long
#define mp make_pair


//code of suren
VI chars; VVI grid;

bool found(VI &cur , int num){
  REP(i,cur.sz) REP(j,grid[cur[i]].sz) if(num==grid[cur[i]][j])  return true;
  return false;
}

void go(VI &cur , VI &seen){
  if(cur.sz == chars.sz) { string ret=""; REP(i,cur.sz) ret+='a'+cur[i]; cout<<ret<<endl; return; }
  seen[cur[cur.sz-1]]=1;
  REP(i,chars.sz) if( seen[chars[i]]==0 && !found(cur,chars[i]) ) {
    cur.pb(chars[i]);
    go(cur,seen);
    cur.pop_back();
  }
  seen[cur[cur.sz-1]]=0;
}

int main(){
  string s1,s2;
  int isfirst = 0;
  while(getline(cin,s1) && getline(cin,s2)){
    if(isfirst++) cout<<endl;
    grid.clear(); grid.resize(26); chars.clear(); 
    istringstream i1(s1),i2(s2); char a,b;
    while(i1>>a) chars.pb(a-'a');
    while(i2>>a>>b) grid[b-'a'].pb(a-'a');
    sort(ALL(chars));
    VI seen(26,-1);
    REP(i,chars.sz) seen[chars[i]]=0;
    VI cur;
    REP(i,chars.sz) { cur.pb(chars[i]); go(cur,seen); cur.pop_back(); }
  }
  return 0;
}
